package com.cardone.platform.configuration.dao;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.configuration.dto.DictionaryTypeDto;
import com.google.common.collect.Maps;

/**
 * 字典类型
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class DictionaryTypeJdbcDao implements DictionaryTypeDao {
	@Override
	public int deleteByIds(final DictionaryTypeDto deleteDictionaryType) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteDictionaryType.getAttrs())) {
			model.putAll(deleteDictionaryType.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteDictionaryType.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final DictionaryTypeDto findDictionaryType) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findDictionaryType, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findDictionaryType, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findDictionaryType.getAttrs())) {
			model.putAll(findDictionaryType.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final DictionaryTypeDto findDictionaryType) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findDictionaryType);

		final Model model = ModelUtils.newModel(new ModelArgs(findDictionaryType, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findDictionaryType.getAttrs())) {
			model.putAll(findDictionaryType.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final DictionaryTypeDto findListDictionaryType) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListDictionaryType).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListDictionaryType.getAttrs())) {
			model.putAll(findListDictionaryType.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public <P> List<P> findListByParentCode(final Class<P> mappedClass, final String parentCode) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.parentCode.name(), parentCode);

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByParentCodeOrParentId.id(), model);
	}

	@Override
	public <P> List<P> findListByParentId(final Class<P> mappedClass, final String parentId) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.parentId.name(), parentId);

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByParentCodeOrParentId.id(), model);
	}

	@Override
	public int insertByCode(final DictionaryTypeDto insertDictionaryType) {
		if (StringUtils.isBlank(insertDictionaryType.getId())) {
			insertDictionaryType.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertDictionaryType));

		if (MapUtils.isNotEmpty(insertDictionaryType.getAttrs())) {
			model.putAll(insertDictionaryType.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final DictionaryTypeDto paginationDictionaryType) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationDictionaryType).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationDictionaryType.getAttrs())) {
			model.putAll(paginationDictionaryType.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationDictionaryType), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final DictionaryTypeDto readDictionaryType) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readDictionaryType, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readDictionaryType.getAttrs())) {
			model.putAll(readDictionaryType.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readDictionaryType);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readDictionaryType, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final DictionaryTypeDto saveDictionaryType) {
		final P oldDictionaryType = this.findByCodeOrId(mappedClass, saveDictionaryType);

		if (oldDictionaryType == null) {
			this.insertByCode(saveDictionaryType);

			return this.findByCodeOrId(mappedClass, saveDictionaryType);
		}

		final int updateCount = this.updateByCode(saveDictionaryType, oldDictionaryType);

		if (updateCount < 1) {
			return oldDictionaryType;
		}

		return this.findByCodeOrId(mappedClass, saveDictionaryType);
	}

	@Override
	public int updateByCode(final DictionaryTypeDto updateDictionaryType) {
		final DictionaryTypeDto oldDictionaryType = this.findByCodeOrId(DictionaryTypeDto.class, updateDictionaryType);

		return this.updateByCode(updateDictionaryType, oldDictionaryType);
	}

	private <P> int updateByCode(final DictionaryTypeDto updateDictionaryType, final P oldDictionaryType) {
		if (oldDictionaryType == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.orderNum.name() }, updateDictionaryType);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateDictionaryType, oldDictionaryType, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldDictionaryType, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldDictionaryType, updateDictionaryType, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateDictionaryType, updateProperties), new ModelArgs(updateDictionaryType, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateDictionaryType.getAttrs())) {
			model.putAll(updateDictionaryType.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}